var totalPage = 0;
var totalCnt = 0;
var chargingStationUpgradeData;
var confirmFlag = undefined;
var upgradeIndex = undefined;
var onlineStatus = [];
var upgradeStatus = undefined;
var t = undefined;

$(document).ready(function() {
    showLoading();
    $('title', parent.document).html($('title').text());
    setBaseUrl();
    $ajaxList('chargingStation', getChargingStation);
    createTableHead("upgradeThead", chargingStationUpgradeTable, search);
    scrollTableSuspend("scrollBody", "chargingStationUpgradeTable", "chargingStationUpgradeTable1", "upgradeThead", "scrollHeight");
    search();
    setTimeout(hideLoading, 500);
});
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
function search(){
	onlineStatus = [];
	clearTimeout(t);
	$ajax('searchChargingStationUpgrade', 'chargingStationUpgradeForm', searchChargingStationUpgrade);
}
function searchChargingStationUpgrade(data){
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	chargingStationUpgradeData = data && data.chargingStationUpgradeList || [];
	var length = chargingStationUpgradeData.length;
	for (var i = 0;i < length; i++){
		chargingStationUpgradeData[i].chargingStationName = '<a class="details" onclick="chargingStationName('+i+')">'+chargingStationUpgradeData[i].chargingStationName+'</a>'; 
	}
	createTableBody("upgradeTbody", chargingStationUpgradeTable, chargingStationUpgradeData);
	searchUpgradeStatus();
}
function searchUpgradeStatus(){
	$ajax('getRealTimeUpgradeStatus', '', getRealTimeUpgradeStatus, '', searchUpgradeStatus);
}
function getRealTimeUpgradeStatus(data){
	chargingStationUpgradeData = data && data.chargingStationUpgradeList || [];
	var length = chargingStationUpgradeData.length;
	for(var i=0; i<length; i++){
		if ((onlineStatus[i] === undefined && chargingStationUpgradeData[i].onlineStatus) || 
				(!onlineStatus[i] && chargingStationUpgradeData[i].onlineStatus)) {
			onlineStatus.push(true);
			$('#onlineStatus' + i).html("正常");
			if (upgradeStatus == "START") {
				$("#upgradeFileList" + i).attr('disabled', true);
			} else {
				$("#upgradeFileList" + i).attr('disabled', false);
			}
			$("#upgrade" + i).empty();
			$("#upgrade" + i).append('<a onclick="upgrade(' + i + ')" class="details"><img style="width:25px;height:25px;" src="static/images/upgrade.png"/></a>');
			$('#currentVersion' + i).html(chargingStationUpgradeData[i].currentVersion || '-');
		} else if ((onlineStatus[i] === undefined && !chargingStationUpgradeData[i].onlineStatus) || 
				(onlineStatus[i] && !chargingStationUpgradeData[i].onlineStatus)) {
			onlineStatus.push(false);
			$('#onlineStatus' + i).html("断开");
			$("#upgradeFileList" + i).attr('disabled', true);
			$("#upgrade" + i).empty();
			$("#upgrade" + i).append('<img style="width:25px;height:25px;" src="static/images/upgrading.png"/>');
		}
		if (chargingStationUpgradeData[i].upgradeStatus == "END" && upgradeStatus != "END") {
			upgradeStatus = "END";
			$('#upgradeStatus' + i).html('<img style="width: 24px;height: 24px;" src="static/images/complete.png"/>');
			$("#upgrade" + i).empty();
			$("#upgrade" + i).append('<a onclick="upgrade(' + i + ')" class="details"><img style="width:25px;height:25px;" src="static/images/upgrade.png"/></a>');
			$("#upgradeFileList" + i).attr('disabled', false);
			showMessge(messageContent["Upgrade.Success"]);
		} else if (chargingStationUpgradeData[i].upgradeStatus == "TIMEOUT" && upgradeStatus != "TIMEOUT") {
			upgradeStatus = "TIMEOUT";
			$('#upgradeStatus' + i).html('-');
			showMessge(messageContent["Upgrade.Timeout"]);
		} else if (chargingStationUpgradeData[i].upgradeStatus == "ERROR" && upgradeStatus != "ERROR") {
			upgradeStatus = "ERROR";
			$('#upgradeStatus' + i).html('-');
			showMessge(messageContent["Upgrade.Error"]);
		} else if (chargingStationUpgradeData[i].upgradeStatus == undefined) {
			$('#upgradeStatus' + i).html('-');
		}
	}
	t = setTimeout(searchUpgradeStatus, 2000);
}
function chargingStationName(num){
	window.parent.detalisDrop('chargingStation/chargingStationDetail', num, $("#currentPage").val(), "drownDown1", "" ,"chargingStationPage" ,"chargingStation");
	$("#parentSkipMenu", parent.document).val("chargingStationPage");
	$("#detailID", parent.document).val(chargingStationUpgradeData[num].chargingStationID);
}
function getVersion(i){
	var upgradeFileName = $('#upgradeFileList' + i + " option:selected").text();
	var reg = /(\d+).*(\d+)/g;
	var strName = upgradeFileName.match(reg);
	if (strName) {
		$('#upgradeVersion' + i).html(strName);
	} else {
		$('#upgradeVersion' + i).html('-');
	}
}
function upgrade(index) {
	if (chargingStationUpgradeData && chargingStationUpgradeData[index] && chargingStationUpgradeData[index].onlineStatus) {
		var upgradeFile = $('#upgradeFileList' + index + " option:selected").val();
		if (upgradeFile) {
			showMessge(messageContent["Upgrade.Confirm"], "model2");
			confirmFlag = "upgrade";
			upgradeIndex = index;
		}else {
			$("#upgradeFileList" + index).tips({
				side : 2,
				msg : "请选择升级的文件",
				bg : '#AE81FF',
				time : 3
			});
			$("#upgradeFileList" + index).focus();
		}
	} else {
		showMessge(messageContent["Upgrade.Offline"]);
	}
}
function getUrl(){
	if(confirmFlag == "upgrade"){
		return 'chargingStationUpgrade';
	}
}
function getData(){
	return {
		chargingStationID: chargingStationUpgradeData && chargingStationUpgradeData[upgradeIndex] && chargingStationUpgradeData[upgradeIndex].chargingStationID,
		downloadFile: $('#upgradeFileList' + upgradeIndex + " option:selected").val(),
		upgradeVersion: $('#upgradeVersion' + upgradeIndex).text()
	};
}
function sure(data) {
	var result = data && data.result;
	if (result == "Sucess") {
		upgradeStatus = "START";
		$("#upgradeFileList" + upgradeIndex).attr('disabled', true);
		$("#upgradeStatus" + upgradeIndex).html('<img style="width:25px;height:25px;" src="static/images/wait.gif"/>');
		$("#upgrade" + upgradeIndex).empty();
		$("#upgrade" + upgradeIndex).append('<img style="width:25px;height:25px;" src="static/images/upgrading.png"/>');
		showMessge(messageContent["Upgrade.Upgrading"]);
		upgradeIndex = undefined;
	} else if (result == "NotExist") {
		showMessge(messageContent["Upgrade.NotExist"]);
	} else if (result == "Offline") {
		showMessge(messageContent["Upgrade.Offline"]);
	}
}
function download() {
	$ajax('subStation/download', {chargingStationID: "01"});
}
function upload() {
	$("#uploadUpgradeFileForm").empty();
	$("#uploadUpgradeFileForm").append(
		'<div class="form-group">' + 
        '<label for="upgradeFile" class="col-sm-3 control-label">升级文件</label>' +
        '<div class="col-sm-8"><input id="upgradeFile" name="upgradeFile" type="file" class="form-control"/></div>' +
        '</div>'
	);
	var option = {
			allowedFileExtensions: ['jar', 'war'],
			showUpload: false,
			showErrorPreview: false
		};
	selectFile("upgradeFile", option);
	$("#fileinputModal").modal("show");
}
function confirm() {
	var upgradeFile = $("#upgradeFile").val();
	if (!upgradeFile) {
		$("#upgradeFile").tips({
			side : 1,
			msg : "请选择您要上传的文件",
			bg : '#AE81FF',
			time : 3
		});
		$("#upgradeFile").focus();
	} else {
		showLoading();
		$ajaxSubmit('uploadUpgradeFileForm', 'uploadUpgradeFile', uploadUpgradeFile);
	}
}
function uploadUpgradeFile(data) {
	setTimeout(hideLoading, 500);
	$("#fileinputModal").modal("hide");
	var result = data && data.result;
	if (result == "Sucess") {
		showMessge(messageContent["Upload.Success"]);
		search();
	} else if (result == "NotFound") {
		showMessge(messageContent["Upload.NotFound"]);
	} else {
		showMessge(messageContent["Upload.Failed"]);
	}
}
